package com.mdp.im.core.ctrl;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.mdp.core.utils.BaseUtils;
import com.mdp.core.utils.RequestUtils;
import com.mdp.safe.client.service.remote.UserBaseInfoRemoteQueryService;
import io.swagger.v3.oas.annotations.Operation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mdp.core.entity.Result;
import com.mdp.core.query.QueryTools;
import com.mdp.core.entity.LangTips;
import com.mdp.safe.client.entity.User;
import com.mdp.safe.client.utils.LoginUtils;
import com.mdp.swagger.ApiEntityParams;

import com.mdp.im.core.service.ImUserService;
import com.mdp.im.core.entity.ImUser;
/**
 * @author 唛盟开源 code-gen
 * @since 2024-8-14
 */
@RestController
@RequestMapping(value="/mdp/im/core/imUser")
@Api(tags={"用户-操作接口"})
public class ImUserController {
	
	static Logger logger =LoggerFactory.getLogger(ImUserController.class);

    @Autowired
    UserBaseInfoRemoteQueryService userBaseInfoRemoteQueryService;
	
	@Autowired
	private ImUserService imUserService;

	@Operation( summary =  "用户-查询列表")
	@ApiEntityParams(ImUser.class)
	@RequestMapping(value="/list",method=RequestMethod.GET)
	public Result listImUser(  @RequestParam Map<String,Object> params){
			User user=LoginUtils.getCurrentUserInfo();
			QueryWrapper<ImUser> qw = QueryTools.initQueryWrapper(ImUser.class , params);
			IPage page=QueryTools.initPage(params);
			List<Map<String,Object>> datas = imUserService.selectListMapByWhere(page,qw,params);
			return Result.ok("query-ok","查询成功").setData(datas).setTotal(page.getTotal());
	}
	

	@Operation( summary =  "用户-新增")
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public Result addImUser(@RequestBody ImUser imUser) {
		 imUserService.save(imUser);
         return Result.ok("add-ok","添加成功！").setData(imUser);
	}

	@Operation( summary =  "用户-删除")
	@RequestMapping(value="/del",method=RequestMethod.POST)
	public Result delImUser(@RequestBody ImUser imUser){
		imUserService.removeById(imUser);
        return Result.ok("del-ok","删除成功！");
	}

	@Operation( summary =  "用户-修改")
	@RequestMapping(value="/edit",method=RequestMethod.POST)
	public Result editImUser(@RequestBody ImUser imUser) {
		imUserService.updateById(imUser);
        return Result.ok("edit-ok","修改成功！");
	}

    @Operation( summary =  "用户-批量修改某些字段" )
    @ApiEntityParams( value = ImUser.class, props={ }, remark = "用户", paramType = "body" )
	@RequestMapping(value="/editSomeFields",method=RequestMethod.POST)
	public Result editSomeFields(   @RequestBody Map<String,Object> params) {
            User user= LoginUtils.getCurrentUserInfo();
            imUserService.editSomeFields(params);
            return Result.ok("edit-ok","更新成功");
	}

	@Operation( summary =  "用户-批量删除")
	@RequestMapping(value="/batchDel",method=RequestMethod.POST)
	public Result batchDelImUser(@RequestBody List<ImUser> imUsers) {
	    User user= LoginUtils.getCurrentUserInfo();
        if(imUsers.size()<=0){
            return Result.error("batchDel-data-err-0","请上送待删除数据列表");
        }
         List<ImUser> datasDb=imUserService.listByIds(imUsers.stream().map(i-> i.getUserid() ).collect(Collectors.toList()));

        List<ImUser> can=new ArrayList<>();
        List<ImUser> no=new ArrayList<>();
        for (ImUser data : datasDb) {
            if(true){
                can.add(data);
            }else{
                no.add(data);
            }
        }
        List<String> msgs=new ArrayList<>();
        if(can.size()>0){
            imUserService.removeByIds(can);
            msgs.add(LangTips.transMsg("del-ok-num","成功删除%s条数据.",can.size()));
        }

        if(no.size()>0){
            msgs.add(LangTips.transMsg("not-allow-del-num","以下%s条数据不能删除:【%s】",no.size(),no.stream().map(i-> i.getUserid() ).collect(Collectors.joining(","))));
        }
        if(can.size()>0){
             return Result.ok(msgs.stream().collect(Collectors.joining()));
        }else {
            return Result.error(msgs.stream().collect(Collectors.joining()));
        }
	} 

	@Operation( summary =  "用户-根据主键查询一条数据")
    @RequestMapping(value="/queryById",method=RequestMethod.GET)
    public Result queryById(ImUser imUser) {
        ImUser data = (ImUser) imUserService.getById(imUser);
        return Result.ok().setData(data);
    }


    @RequestMapping(value="/listIsMyFriend",method=RequestMethod.GET)
    public Result listImUserIsMyFriend( @RequestParam Map<String,Object> imUser){
        Map<String,Object> m = new HashMap<>();
        RequestUtils.transformArray(imUser, "userids");
        List<Map<String,Object>>	imUserList = imUserService.listImUserIsMyFriend(imUser);
        return Result.ok().setData(imUserList);
    }

    //sys登录时,就会校验im_user表,没有就会保存进im_user表
    @RequestMapping(value="/synchroSysToImUser",method=RequestMethod.POST)
    public Result synchroSysToImUser(@RequestBody Map<String,Object> map) {
            //根据sys_user表,查找im_user是否有那条数据
            String userid=(String)map.get("userid");
            if (imUserService.count(Wrappers.lambdaQuery(ImUser.class).eq(ImUser::getUserid,userid))>0){
                User user=userBaseInfoRemoteQueryService.getUserByUserid(userid,null);
                ImUser imUserA = BaseUtils.fromMap(user, ImUser.class);
                imUserService.insert(imUserA);
            }
            return Result.ok();
    }

}
